package com.weavernorth.util;


import weaver.conn.RecordSet;
import weaver.docs.docs.DocComInfo;
import weaver.docs.docs.DocImageManager;
import weaver.docs.docs.DocManager;
import weaver.docs.docs.DocViewer;
import weaver.file.ImageFileManager;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;



/**
 * chmod  777
 * Created by Administrator on 2017-8-21.
 */
public class CreateFileUtilForImage {
    /**
     *
     * @param pathlst 附件路径
     * @param secid     目录id
     * @param createrid 创建人id
     * @param imagefilename1 附件名称
     *
     *
     * @param namelst 附件名称集合
     * @param imagefileidlst 附件id集合
     * @return
     */
    BaseBean _bb=new BaseBean();
    public int doCreate(List<String> pathlst,List<String>  namelst,List<String> imagefileidlst,String secid,String createrid,String imagefilename1){
    	RecordSet  rs  = new RecordSet();
    	rs.writeLog("CreateFileUtilForImage   star");
        if("".equals(secid)){
            return -101;
        }
        if("".equals(createrid)){
            return -102;
        }
        if("".equals(imagefilename1)){
            return -103;
        }
        int sender = Integer.parseInt(createrid);
        int seccategory = Integer.parseInt(secid);
        List<String> imgidlst = new ArrayList<String>();
        for(int x = 0;x<pathlst.size();x++){
        	String filepath = pathlst.get(x);   
        	String imagefilename = namelst.get(x);
        	int imagefileid = 0;
        	byte []content = null;
        	try {
        		// 创建输入流和内存输出流
        		int byteread;
        		byte data[] = new byte[1024];
        		InputStream input = new FileInputStream(new File(filepath));
        		ByteArrayOutputStream out = new ByteArrayOutputStream();
        		while ((byteread = input.read(data)) != -1) {
        			out.write(data, 0, byteread);
        			out.flush();
        		}
        		content = out.toByteArray();
        		input.close();
        		out.close();
        		imagefileid = getImageFile(content,imagefilename);
        		imgidlst.add(imagefileid+"");
        	} catch (Exception e) {
        		e.printStackTrace();
        		_bb.writeLog("-----------"+e.getMessage());
        		return -104;
        	}
        }
        for(int x = 0;x<imagefileidlst.size();x++){
        	InputStream inputStream=null;	
        	ImageFileManager imageFileManager=new ImageFileManager();
 		    imageFileManager.getImageFileInfoById(Util.getIntValue(imagefileidlst.get(x)));
 		    inputStream = imageFileManager.getInputStream();
 		    String tempimgnameString="";
 		    rs.executeQuery(" select a.imagefilename from imagefile a where a.imagefileid = "+imagefileidlst.get(x));
 		    if(rs.next()){
 		    	tempimgnameString = rs.getString("imagefilename");
 		    }
        	int imagefileid = 0;
        	byte []content = null;
        	try {
        		// 创建输入流和内存输出流
        		int byteread;
        		byte data[] = new byte[1024];
        		ByteArrayOutputStream out = new ByteArrayOutputStream();
        		while ((byteread = inputStream.read(data)) != -1) {
        			out.write(data, 0, byteread);
        			out.flush();
        		}
        		content = out.toByteArray();
        		inputStream.close();
        		out.close();
        		imagefileid = getImageFile(content,tempimgnameString);
        		imgidlst.add(imagefileid+"");
        	} catch (Exception e) {
        		e.printStackTrace();
        		return -104;
        	}
        }
        int docid = getDoc(imagefilename1,imgidlst,namelst,seccategory,sender);
        return docid;
    }

    //生成附件
    private int getImageFile(byte []content, String name){
        ImageFileManager imageFileManager = new ImageFileManager();
        imageFileManager.resetParameter();
        imageFileManager.setData(content);
        imageFileManager.setImagFileName(name);
        imageFileManager.setComefrom(" from accessary");
        int imagefileid = imageFileManager.saveImageFile();
        return imagefileid;
    }

    //获取文档
    private int getDoc(String imagefilename1,List<String> imgidlst,List<String> namelst,int seccategory,int sender){
        RecordSet rs = new RecordSet();
        DocComInfo dc = new DocComInfo();
        DocViewer dv = new DocViewer();
        DocManager docManager = new DocManager();
        DocImageManager imgManger = new DocImageManager();
        int docid = 0;
        try {
             docid = docManager.getNextDocId(rs);
            docManager.setSeccategory(seccategory);
          
            docManager.setId(docid);
            docManager.setDocextendname("zip");
            for(int x=0;x<imgidlst.size();x++){
            	String filename = "";
            	String imagefileid = imgidlst.get(x);
            	rs.executeSql("select imagefilename from imagefile where imagefileid=" + imagefileid);
                if(rs.next()) {
                   filename = rs.getString(1);
                }
                String filename_bak = filename;
                if(filename.indexOf(".") > 0) {
                    filename_bak = filename.substring(0, filename.lastIndexOf("."));
                }
                imgManger.resetParameter();
                imgManger.setDocid(docid);
                imgManger.setImagefileid(Util.getIntValue(imagefileid+"", 0));
                imgManger.setImagefilename(filename);
                imgManger.setIsextfile("1");
                String ext = docManager.getFileExt(filename);
                if(ext.equalsIgnoreCase("doc")) {
                    imgManger.setDocfiletype("3");
                } else if(ext.equalsIgnoreCase("xls")) {
                    imgManger.setDocfiletype("4");
                } else if(ext.equalsIgnoreCase("ppt")) {
                    imgManger.setDocfiletype("5");
                } else if(ext.equalsIgnoreCase("wps")) {
                    imgManger.setDocfiletype("6");
                } else if(ext.equalsIgnoreCase("docx")) {
                    imgManger.setDocfiletype("7");
                } else if(ext.equalsIgnoreCase("xlsx")) {
                    imgManger.setDocfiletype("8");
                } else if(ext.equalsIgnoreCase("pptx")) {
                    imgManger.setDocfiletype("9");
                } else if(ext.equalsIgnoreCase("et")) {
                    imgManger.setDocfiletype("10");
                } else {
                    imgManger.setDocfiletype("2");
                }
                imgManger.AddDocImageInfo();
            }
            docManager.setDoccontent("");
            Calendar today = Calendar.getInstance();
            String formatdate = Util.add0(today.get(1), 4) + "-" + Util.add0(today.get(2) + 1, 2) + "-" + Util.add0(today.get(5), 2);
            String formattime = Util.add0(today.get(11), 2) + ":" + Util.add0(today.get(12), 2) + ":" + Util.add0(today.get(13), 2);
            docManager.setDoccreatedate(formatdate);
            docManager.setDoccreatetime(formattime);
            docManager.setDoccreaterid(sender);
            docManager.setOwnerid(sender);
            docManager.setDoclastmoduserid(sender);
            docManager.setDoclastmoddate(formatdate);
            docManager.setDoclastmodtime(formattime);
            docManager.setSeccategory(seccategory);
            docManager.setDocstatus("1");
            docManager.setAccessorycount(1);
            docManager.setUserid(sender);
            int tempPos = imagefilename1.lastIndexOf(".");
            System.out.println("imagefilename1i="+imagefilename1+"\n");
            if(tempPos == -1) {
                docManager.setDocsubject(imagefilename1);
            } else {
                docManager.setDocsubject(imagefilename1.substring(0, tempPos));
            }
            docManager.AddDocInfo();
            docManager.AddShareInfo();
            dc.addDocInfoCache("" + docid);
            dv.setDocShareByDoc("" + docid);
            rs.executeSql(" update docdetail set accessorycount = "+
            			  " (select count(distinct id) from DocImageFile where isextfile = '1' and docid = " 
            			  + docid + " and docfiletype <> '1'   and docfiletype <> '11') where id = " + docid);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return docid;
    }

 
}
